package com.itheima.stock.security.service;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.stock.constant.PermissionConstant;
import com.itheima.stock.face.StockBackendCacheFace;
import com.itheima.stock.mapper.SysPermissionMapper;
import com.itheima.stock.mapper.SysRoleMapper;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.mapper.SysUserRoleMapper;
import com.itheima.stock.pojo.domain.UserDomain;
import com.itheima.stock.pojo.dto.PermissionsDTO;
import com.itheima.stock.pojo.entity.SysRole;
import com.itheima.stock.pojo.entity.SysUser;
import com.itheima.stock.pojo.vo.resp.PermissionsVO;
import com.itheima.stock.pojo.vo.resp.ResponseCode;
import com.itheima.stock.process.DataProcess;
import com.itheima.stock.security.user.LoginUserDetail;
import com.itheima.stock.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


import java.util.ArrayList;
import java.util.List;


@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private StockBackendCacheFace stockBackendCacheFace;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser dbUser = sysUserMapper.findUserInfoByUserName(username);
        if (dbUser == null) {
            throw new UsernameNotFoundException(String.valueOf(ResponseCode.ACCOUNT_NOT_EXISTS));
        }
        //获取权限
        //4.响应
        UserDomain userDomain = new UserDomain();
        //获取用户信息
        BeanUtils.copyProperties(dbUser, userDomain);
        //通过用户id获取角色
        List<String> role = stockBackendCacheFace.getRole(userDomain.getId());
        //通过用户id获取所有按钮
        List<String> code = stockBackendCacheFace.getCode(userDomain.getId());
        //通过用户id获取目录
        List<String> perms = stockBackendCacheFace.getPerms(userDomain.getId());
        List<PermissionsVO> permissionVOs = stockBackendCacheFace.getPermissionsVO(userDomain.getId());
        //将权限装载给角色
        userDomain.setMenus(permissionVOs);
        //通过角色所有按钮
        userDomain.setPermissions(code);
        //角色和权限组装在一起
        ArrayList<String> ps = new ArrayList<>();
        ps.addAll(perms);
        ps.addAll(role);
        String[] array = ps.toArray(new String[0]);
        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(array);
        LoginUserDetail userDetail = new LoginUserDetail();
        BeanUtils.copyProperties(userDomain, userDetail);
        userDetail.setAuthorities(authorities);
        userDetail.setPassword(dbUser.getPassword());
        return userDetail;//装载完user去BCryptPasswordEncoder比对密码,匹配成功后进入MyUsernamePasswordAuthenticationFilter执行success方法
    }
}
